<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Base2
{
	var $title = "Basis";
	var $toolbar = false;
	var $moduleID = 0;
	var $permission;
	var $allowed = true;
	var $filter = "";
	var $toolbarClass = "icon-48-article";
	var $error = false;
	var $errorText = "";
	var $dataTable;
	var $data;
	var $hasData;
	var $actual;
	var $smarty;
	var $db;
	var $search;
	var $selectSql;
	
	public function Base()
	{
		session_start();
		$this->db = new Database();
	}
	
	public function AnalysPost()
	{
		
	}
	
	public function Add()
	{
		
	}
	
	public function PaintEditPanel()
	{
		if($this->dataTable == "")
		{
			alert("Base->dataTable cannot be null");
			return;
		}
		
		if($this->selectSql == "")
		{
			$this->selectSql = "SELECT * FROM ".$this->dataTable;
		}
		
		$this->smarty = GetSmarty();
		$this->smarty->assign("errorField",  false);
		$id = $_POST['id'];
		$this->search = $_POST['search'];
		
		if($_POST['action'] == "first")
		{
			$this->First();
		}
		else if($_POST['action'] == "last")
		{
			$this->Last();
		}
		else if($_POST['action'] == "right")
		{
			$this->Right();
		}
		else if($_POST['action'] == "left")
		{
			$this->Left();
		}
		else if($_POST['action'] == "save" || $_POST['action'] == "update")
		{
			$this->OnSaveUpdate();
		}
		else if($_POST['action'] == "search")
		{
			$this->Search();
		}
		else if($_POST['action'] == "clear")
		{
			$this->Clear();
		}
		else
		{
			$this->SetDisabledFields(true, false, false, true);
		}
		
		$this->actual = ($this->actual == "") ? 0 : $this->actual;
		
		$this->smarty->assign("countText",$this->actual."/".($GLOBALS['db']->GetCount($this->dataTable, $this->search)));
		$this->smarty->assign('smartyHiddenFields', array( 
				array('name' => 'count','value' => $GLOBALS['db']->GetCount($this->dataTable,  $this->search)),
                array('name' => 'actualData','value' => $this->actual)));
		
		
	}
	
	private function SetDisabledFields($first, $left, $right, $last)
	{
		$this->smarty->assign("first",  $first);
		$this->smarty->assign("left",  $left);
		$this->smarty->assign("right",  $right);
		$this->smarty->assign("last",  $last);
	}

	private function BuildQuery($sql, $where, $order)
	{
		if($this->search != "")
		{
			
			return $sql.$where.$this->search.$order;
		}
		else
		{
			return $sql.$where.$order;
		}
	}
	
	
	private function Clear()
	{
		$this->search = "";
		$_POST['search'] = "";
		$this->SetDisabledFields(true, false, false, true);
		$this->actual = 0;
	}
	
	private function Search()
	{
		$this->search = "";
		$this->search = " WHERE ".$GLOBALS['db']->SearchObject($this->dataTable, true);
		
		$this->actual = 1;
		
		$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID LIMIT 0,1"));
		
		if($GLOBALS['db']->NumRow($res))
		{
			$this->hasData = true;
			$this->data = $GLOBALS['db']->Fetch($res);
			
			if($GLOBALS['db']->GetCount($this->dataTable, $this->search) > 1)
			{
				$this->SetDisabledFields(false, false, true, true);
			}
			else
			{
				$this->SetDisabledFields(false, false, false, false);
			}
		}
		else
		{
			//nothing found
		}
	}
	
	private function OnSaveUpdate()
	{
		if($_POST['action'] == "add")	
		Alert($_POST['id']);
		$this->SetDisabledFields(false, false, false, false);
		$this->actual = $_POST['actualData'];
			
		if($_POST['action'] == "update")
			$res = $GLOBALS['db']->Query($this->selectSql." WHERE id =".$_POST['id']);
		else
			$res = $GLOBALS['db']->Query($this->selectSql." ORDER BY ID desc LIMIT 0,1");

		if($GLOBALS['db']->NumRow($res))
		{
			$this->hasData = true;
			$this->data = $GLOBALS['db']->Fetch($res);
		}	
	}
	
	
	private function First()
	{
		$this->SetDisabledFields(false, false, true, true);
		$this->actual = 1;
		$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID LIMIT 0,1"));
		
		if($GLOBALS['db']->NumRow($res))
		{
			$this->hasData = true;
			$this->data = $GLOBALS['db']->Fetch($res);
		}
	}
	
	private function Last()
	{
		$this->SetDisabledFields(true, true, false, false);
		$this->actual = $GLOBALS['db']->GetCount($this->dataTable, $this->search);
		$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID desc LIMIT 0,1"));
		
		if($GLOBALS['db']->NumRow($res))
		{
			$this->hasData = true;
			$this->data = $GLOBALS['db']->Fetch($res);
		}
	}
	
	private function Right()
	{
		if($_POST['actualData']+1 == $GLOBALS['db']->GetCount($this->dataTable,  $this->search))
		{
			$this->SetDisabledFields(true, true, false, false);
			$this->actual = $GLOBALS['db']->GetCount($this->dataTable, $this->search);
			$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID desc LIMIT 0,1"));
			
			if($GLOBALS['db']->NumRow($res))
			{
				$this->hasData = true;
				$this->data = $GLOBALS['db']->Fetch($res);
			}
		}
		else
		{
			$this->SetDisabledFields(true, true, true, true);
			
			$this->actual = $_POST['actualData']+1;
			$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID LIMIT ".$_POST['actualData'].",1"));
			
			if($GLOBALS['db']->NumRow($res))
			{
				$this->hasData = true;
				$this->data = $GLOBALS['db']->Fetch($res);
			}
		}
	}
	
	private function Left()
	{
		if($_POST['actualData']-1 == 1)
		{
			$this->SetDisabledFields(false, false, true, true);
			$this->actual = 1;
			$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID asc LIMIT 0,1"));
			
			if($GLOBALS['db']->NumRow($res))
			{
				$this->hasData = true;
				$this->data = $GLOBALS['db']->Fetch($res);
			}
		}
		else
		{
			$this->SetDisabledFields(true, true, true, true);
			$this->actual = $_POST['actualData']-1;
			
			$res = $GLOBALS['db']->Query($this->BuildQuery($this->selectSql, "", " ORDER BY ID asc LIMIT ".($this->actual-1).",1"));
			if($GLOBALS['db']->NumRow($res))
			{
				$this->hasData = true;
				$this->data = $GLOBALS['db']->Fetch($res);
			}
		}
	}
	
	
	public function Toolbar()
	{
		
	}

	public function AddCSS()
	{
			
	}
	
	public function AddJS()
	{
		
	}
}

?>